package cn.wolfcode.web.controller;

import cn.wolfcode.MyEnum.ConsumptionEnum;
import cn.wolfcode.MyEnum.ConsumptionReportEnum;
import cn.wolfcode.qo.ConsumptionReportQueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionReportService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/consumptionReport")
public class consumptionReportController {

    @Autowired
    private IConsumptionReportService consumptionReportService;
    @Autowired
    private IBusinessService businessService;



    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") ConsumptionReportQueryObject queryObject){
        PageInfo<HashMap> pageInfo = consumptionReportService.query(queryObject);
        List<HashMap> list = pageInfo.getList();
        model.addAttribute("HashMapperList",list);
        ConsumptionReportEnum[] values = ConsumptionReportEnum.values();
        model.addAttribute("ConReportEnum", values);
        model.addAttribute("ConEnum", ConsumptionEnum.values());
        model.addAttribute("business", businessService.listAll());
        return "consumptionReport/list";
    }

    @RequestMapping("/bar")
    public String bar(Model model,@ModelAttribute("qo") ConsumptionReportQueryObject queryObject){
        //把PageSize设置未0表示的就是不进行分页,查询全部
        queryObject.setPageSize(0);
        List<HashMap> list = consumptionReportService.query(queryObject).getList();
        //定义五个容器用于把MAP中的各项数据分类
        ArrayList<Object> xList = new ArrayList<>();
        ArrayList<Object> numberList = new ArrayList<>();
        ArrayList<Object> totalAmountList = new ArrayList<>();
        ArrayList<Object> payAmountList = new ArrayList<>();
        ArrayList<Object> discountAmountList = new ArrayList<>();
        for (HashMap hashMap : list) {
            xList.add(hashMap.get("baseType"));
            numberList.add(hashMap.get("number"));
            totalAmountList.add(hashMap.get("totalAmount"));
            payAmountList.add(hashMap.get("payAmount"));
            discountAmountList.add(hashMap.get("discountAmount"));
        }
        model.addAttribute("baseType", JSON.toJSONString(xList));
        model.addAttribute("number", JSON.toJSONString(numberList));
        model.addAttribute("totalAmount", JSON.toJSONString(totalAmountList));
        model.addAttribute("payAmount", JSON.toJSONString(payAmountList));
        model.addAttribute("discountAmount", JSON.toJSONString(discountAmountList));
        return "consumptionReport/bar";
    }

}
